package com.codingfuture.glacierhardware.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codingfuture.glaciercommon.util.EntityConverter;
import com.codingfuture.glaciercommon.util.PageDate;
import com.codingfuture.glaciercommon.util.PageParam;
import com.codingfuture.glaciercommon.util.Result;
import com.codingfuture.glacierhardware.dto.CompanyAddDTO;
import com.codingfuture.glacierhardware.dto.CompanyUpdateDTO;
import com.codingfuture.glacierhardware.entity.Company;
import com.codingfuture.glacierhardware.service.CompanyService;
import com.codingfuture.glacierhardware.vo.CompanysVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author :zxw
 * @date 2025-03-31 17:04
 * @description:
 */
@RestController
@RequestMapping
@Slf4j
@CrossOrigin
@RefreshScope
public class CompanyController {
    @Autowired
    public CompanyService companyService;
    @GetMapping("/company/selectByPage")
    public Result<PageDate<List<Company>>> selectByPage(String name,PageParam pageParam){
        Page<Company> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
       companyService.selectByPage(name,page);
        PageDate<List<Company>> pageDate = new PageDate<>();
        pageDate.setRows(page.getRecords());
        pageDate.setTotal(page.getTotal());
        log.debug("selectByPage{}",pageParam);
        return Result.successes(pageDate);
    }

    @PostMapping("/company/add")
    public Result<Void> add(@RequestBody CompanyAddDTO companyAddDTO){
        Company company = companyAddDTO.convert2Entity();
        return companyService.add(company)
                ?Result.success()
                :Result.err();

    }
    @PostMapping("/company/update")
    public Result<Void> update(@RequestBody CompanyUpdateDTO companyUpdateDTO){
        Company company = companyUpdateDTO.convert2Entity();
        return  companyService.update(company)
                ?Result.success()
                :Result.err();

    }
@GetMapping("/company/findById")
public Result<CompanysVO> findById(@RequestParam Long id){
    Company company = companyService.findById(id);
    CompanysVO companysVO = EntityConverter.entity2DTO(company, CompanysVO.class);
    return Result.successes(companysVO);
}
    @GetMapping("/company/deleteById")
    public Result<Void> delete(@RequestParam Long id){
       return companyService.deleteById(id)
               ?Result.success()
               :Result.err();
    }
}
